package hero_info

import (
	"go_server/game_server/protocol/pb"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
)

// 添加英雄
func (s *Hero_info_service) S2C_AddHero(conn ziface.IConnection, roleid int64, cfgids []int32) (bool, pb.ErrorCodeManager) {
	hero := Hero_info_serviceObj.LoadByRoleId(roleid)
	cards, ok, code := s.on_S2C_AddHero(hero, cfgids)
	if ok {
		msgS2C := &pb.AddHeroS2C{Heros: cards}
		defer func() {
			if msgS2C != nil {
				zlog.Debugf("ProtocolManager_AddHero %v", msgS2C)
				defer func() {
					conn.SendBuffPBMsg(uint32(pb.ProtocolManager_AddHero), msgS2C)
				}()
			}
		}()
	}
	return ok, code
}

func (s *Hero_info_service) on_S2C_AddHero(hero *Cache_hero_info, cfgids []int32) ([]*pb.HeroCardData, bool, pb.ErrorCodeManager) {
	if cfgids != nil && len(cfgids) > 0 && hero != nil {
		pbCards := make([]*pb.HeroCardData, 0)
		for _, cid := range cfgids {
			if hv, ok, _ := hero.AddHero(cid); ok {
				pbCards = append(pbCards, hv.ToPB_HeroCardData())
			}
		}
		defer func() {
			hero.SaveDB_HeroInfo_All()
		}()
		return pbCards, true, pb.ErrorCodeManager_SUCCESS
	} else {
		return nil, false, pb.ErrorCodeManager_HERO_INFO_NOT_ROLEID
	}
}
